package com.woniuxy.controller;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @author: wgt
 * Date: 2021/11/23:22:56
 * Description:45
 */
@Controller
public class FileUpload {
    @RequestMapping("/upload")
    public  String upFile(@RequestParam(value = "file" ,required = false) CommonsMultipartFile file, HttpServletRequest request) throws IOException {
       //获取上传的文件名
//       String upFileName=file.getOriginalFilename();
       //判空
//        if ("".equals(upFileName)){
//            return "文件为空";
//        }
        //上传路径保存设置
        String path=request.getServletContext().getRealPath("/upload");

        File fileName = new File(path);
        if (!fileName.exists()){
            fileName.mkdir();
        }
        System.out.println("上传文件保存地址:"+fileName);
        file.transferTo(new File(fileName+"/"+file.getOriginalFilename()));
         return  "/WEB-INF/success.jsp";
    }

}
